package com.hfzy.ihk.web.aliyuncall.biz;

import com.alibaba.fastjson.JSONObject;
import com.hfzy.ihk.common.util.string.StringTools;
import com.hfzy.ihk.web.aliyuncall.constant.Constants;
import com.hfzy.ihk.web.aliyuncall.support.websocket.WebSocketUtils;
import com.hfzy.ihk.web.aliyuncall.vo.VoiceRTASRReportVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by YANFA on 2018/9/11.
 */
@Component
public class PushDataToBrowserBiz {

    @Autowired
    WebSocketUtils webSocketUtils;

    @Async("pushASRDataExecutor")
    public void pushAsrToBrowser(VoiceRTASRReportVo voiceRTASRReportVo,String callType){
        if(StringTools.isNotempty(voiceRTASRReportVo)&&StringTools.isNotempty(voiceRTASRReportVo.getRole())
                &&StringTools.isNotempty(voiceRTASRReportVo.getOut_id())&&StringTools.isNotempty(voiceRTASRReportVo.getWords())){
            String sessionId = voiceRTASRReportVo.getOut_id();
            Map<String,Object> map = new HashMap<String,Object>();
            map.put(Constants.WEB_SOCKET_JSON_KEY_MSG_TYPE, Constants.WEB_SOCKET_MSG_TYPE_WORDS);
            map.put(Constants.WEB_SOCKET_JSON_KEY_SESSION_ID, voiceRTASRReportVo.getOut_id());
            if(StringTools.isNotempty(voiceRTASRReportVo.getRole())) {
                map.put(Constants.WEB_SOCKET_JSON_KEY_ROLE, voiceRTASRReportVo.getRole().equals("A")?"customer":"broker");
                map.put(Constants.WEB_SOCKET_JSON_KEY_WORDS, voiceRTASRReportVo.getWords());
                map.put(Constants.WEB_SOCKET_JSON_KEY_WORDS_ID,voiceRTASRReportVo.getWordId());
                //将状态推到客户端
                webSocketUtils.pushToClientBySessionId(sessionId, JSONObject.toJSONString(map));
            }


        }

    }


}
